package com.fps.web.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fpswork.common.Constant;
import com.fpswork.core.exception.exception.BusinessException;
import com.fpswork.core.model.PageEntity;
import com.fps.web.dao.BaseUserDao;
import com.fps.web.model.BaseUser;
import com.fps.web.service.BaseUserService;
@Service
@Transactional
public class BaseUserServiceImpl implements BaseUserService{
    
    @Autowired
    BaseUserDao baseUserDao;

    public Map<String, String> loginCheck(BaseUser baseUser,HttpServletRequest request) throws BusinessException{
		Map<String, String> map = new HashMap<String, String>();		
			BaseUser user =new BaseUser();
			user = baseUserDao.getUserByLoginName(baseUser.getUsername());			
			if (null == user) {
				map.put("code", "-1001");
				map.put("msg", "ELoginName");
				return map;
			}
			if (!baseUser.getPassword().equals(user.getPassword())) {
				map.put("code", "-1002");
				return map;
			}
			
			if (user.getStatus().equals(Constant.USER_STATUS.DISABLED.ordinal())) {
				map.put("code", "-1001");
				map.put("msg", "EDisabled");
				return map;
			}						
			map.put("code", "0");		
			if ("0".equals(map.get("code"))) {
				request.getSession().setAttribute(Constant.SESSION_USER_KEY, user);
				request.getSession().setAttribute(Constant.SESSION_USER_TYPE, user.getType());
				request.getSession().setAttribute(Constant.SESSION_USER_NAME, user.getUsername());
				
			}
			return map;

	}
    public PageEntity queryUserList(PageEntity pageEntity)  throws BusinessException{
    	return baseUserDao.queryUserList(pageEntity);
    }
	public BaseUser addUser(BaseUser baseUser)  throws BusinessException{
		return baseUserDao.addUser(baseUser);
	}
	public BaseUser queryUserByID(String id)  throws BusinessException{
		return baseUserDao.queryUserByID(id);
	}
	public Boolean deleteUser(BaseUser baseUser)  throws BusinessException{
		return baseUserDao.deleteUser(baseUser);
	}
}
